package com.czy.daishusys.ui.personal;

import android.content.DialogInterface;
import android.os.Handler;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.czy.daishusys.R;
import com.czy.daishusys.db.UserHelper;
import com.czy.daishusys.global.Constants;
import com.czy.daishusys.ui.BaseActivity;
import com.czy.daishusys.ui.login.LoginActivity;
import com.czy.daishusys.utils.ActivityHelper;
import com.czy.daishusys.utils.PhoneUtils;
import com.czy.daishusys.utils.PicDialog;
import com.czy.daishusys.widget.MyEditText;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author: Raven
 * @date: 2017/11/24 11:35
 * @email: xiexiaomeng_001@163.com
 * @description: 修改用户密码页面
 */

public class ModifyPasswordActivity extends BaseActivity {


    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_subtitle)
    TextView toolbarSubtitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.et_ori_passwd)
    MyEditText etOriPasswd;
    @BindView(R.id.et_new_passwd)
    MyEditText etNewPasswd;
    @BindView(R.id.et_re_new_passwd)
    MyEditText etReNewPasswd;
    @BindView(R.id.btn_ensure)
    Button btnEnsure;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_modify_password;
    }

    @Override
    protected void initInjector() {
        getToolbarTitle().setText(R.string.modify_password_title);
        showBack();
    }

    @Override
    protected void initViews() {

    }


    @OnClick(R.id.btn_ensure)
    protected void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_ensure:
                attemptModify();
                break;
        }
    }

    private void modify( String oriPassword , String newPassword){
        HttpParams params =  new HttpParams();
        params.put("session", UserHelper.getInstance().getSessionInfoModel(this).getSession());
        params.put("oldPassword", oriPassword);
        params.put("newPassword", newPassword);
        params.put("pid",UserHelper.getInstance().getDevicePid());
        mMProgressDialog.show();
        EasyHttp.post(Constants.UPDATE_PASSWD)
                .params(params)
                .timeStamp(true)//添加时间戳
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onSuccess(String s) {
                        mMProgressDialog.dismiss();
                        String data = PhoneUtils.checkResponseFlag(ModifyPasswordActivity.this,s);
                        if(data!=null){

                            PicDialog.Builder builder = new PicDialog.Builder(ModifyPasswordActivity.this)
                                    .setTitleIcon(R.mipmap.ic_dialog_title_passwd)
                                    .setTitle("修改成功");
                            final PicDialog dialog = builder.create();
                            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                                @Override
                                public void onDismiss(DialogInterface dialogInterface) {
                                    ActivityHelper.getInstance().finishAllActivity();
                                    LoginActivity.navToLogin(ModifyPasswordActivity.this);
                                }
                            });
                            dialog.show();

                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    dialog.dismiss();
                                }
                            },1000);

                        }
                    }
                });

    }

    private void attemptModify(){
        etOriPasswd.setError(null);
        etNewPasswd.setError(null);
        etReNewPasswd.setError(null);
        String oriPasswd = etOriPasswd.getText().toString();
        String newPasswd = etNewPasswd.getText().toString();
        String reNewPasswd = etReNewPasswd.getText().toString();

        boolean canModify = true;
        View focusView = null;

        
        if(!reNewPasswd.equals(newPasswd)){
            etReNewPasswd.setError(getString(R.string.modify_password_diff));
            focusView = etReNewPasswd;
            canModify = false;
        }

        if(reNewPasswd.trim().length() == 0){
            etReNewPasswd.setError(getString(R.string.modify_password_ensure_new_hint));
            focusView = etReNewPasswd;
            canModify = false;
        }

        if (newPasswd.length() < 6 ) {
            etNewPasswd.setError(getString(R.string.invalid_password_length_min));
            focusView = etNewPasswd;
            canModify = false;
        }

        if(newPasswd.length() > 20){
            etNewPasswd.setError(getString(R.string.invalid_password_length_max));
            focusView = etNewPasswd;
            canModify = false;
        }


        //检查密码长度
        if (oriPasswd.length() < 6 ) {
            etOriPasswd.setError(getString(R.string.invalid_password_length_min));
            focusView = etOriPasswd;
            canModify = false;
        }

        if(oriPasswd.length() > 20){
            etOriPasswd.setError(getString(R.string.invalid_password_length_max));
            focusView = etOriPasswd;
            canModify = false;
        }

        if (canModify) {
            modify(oriPasswd,newPasswd);
        } else {
            focusView.requestFocus();
        }
    }

    @Override
    protected void updateViews() {

    }

}
